/*
 * PegGameGUI.java
 *
 * Created on October 4, 2007, 10:54 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package peggame;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseAdapter;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 *
 * @author Eric Drucker
 */
public class PegGameGUI extends JFrame implements Runnable
{
	
	private final Thread displayer;
	
	private PegGameBoard p;
	private PegGameAI ai;
	
	private boolean gameOver = false;
	
	public class DisplayPanel extends JPanel
	{
		private Rectangle2D.Double[][] spaces;
		private Ellipse2D.Double[][] myPegs;
		private Ellipse2D.Double currentPeg;
		private boolean movingPeg = false;
		
		private int[] startPeg = new int[]{-1, -1};
		private int[] endPeg = new int[2];
		private boolean validStart = false;
		private boolean validEnd = false;
		
		/**
		 * Panel where game board is drawn
		 */
		public DisplayPanel()
		{
			spaces = new Rectangle2D.Double[][]
			{
				{new Rectangle2D.Double(250,50,100,100)},
				{new Rectangle2D.Double(200,150,100,100),new Rectangle2D.Double(300,150,100,100)},
				{new Rectangle2D.Double(150,250,100,100),new Rectangle2D.Double(250,250,100,100),new Rectangle2D.Double(350,250,100,100)},
				{new Rectangle2D.Double(100,350,100,100),new Rectangle2D.Double(200,350,100,100),new Rectangle2D.Double(300,350,100,100),new Rectangle2D.Double(400,350,100,100)},
				{new Rectangle2D.Double(50,450,100,100),new Rectangle2D.Double(150,450,100,100),new Rectangle2D.Double(250,450,100,100),new Rectangle2D.Double(350,450,100,100),new Rectangle2D.Double(450,450,100,100)}
			};
			
			myPegs = new Ellipse2D.Double[][]
			{
				{new Ellipse2D.Double(255, 55, 90, 90)},
				{new Ellipse2D.Double(205, 155, 90, 90), new Ellipse2D.Double(305, 155, 90, 90)},
				{new Ellipse2D.Double(155, 255, 90, 90), new Ellipse2D.Double(255, 255, 90, 90), new Ellipse2D.Double(355, 255, 90, 90)},
				{new Ellipse2D.Double(105, 355, 90, 90), new Ellipse2D.Double(205, 355, 90, 90), new Ellipse2D.Double(305, 355, 90, 90), new Ellipse2D.Double(405, 355, 90, 90)},
				{new Ellipse2D.Double(55, 455, 90, 90), new Ellipse2D.Double(155, 455, 90, 90), new Ellipse2D.Double(255, 455, 90, 90), new Ellipse2D.Double(355, 455, 90, 90), new Ellipse2D.Double(455, 455, 90, 90)}
			};
			
			currentPeg = new Ellipse2D.Double(-100, -100, 90, 90);
			
			addMouseListener(new java.awt.event.MouseAdapter()
		     {
				public void mouseClicked(java.awt.event.MouseEvent evt)
				{
					int[] firstPeg = new int[2];
			
					int x = evt.getX();
					int y = evt.getY();
			
					boolean validClick = false;
			
					process:
						for(int i = 0; i < spaces.length; i++)
						{
							for(int j = 0; j < spaces[i].length; j++)
							{
								if(x > spaces[i][j].x && x < spaces[i][j].x+spaces[i][j].width && y > spaces[i][j].y && y < spaces[i][j].y+spaces[i][j].height)
								{
									validClick = true;
									firstPeg[0] = i;
									firstPeg[1] = j;
							
									break process; 
								}
							}
						}
					if(validClick)
					{
						p.removeFirstPeg(firstPeg);
						repaint();
					}
			
					removeMouseListener(this);
				}
			});
			
			addMouseMotionListener(new java.awt.event.MouseMotionAdapter()
			{
				public void mouseDragged(java.awt.event.MouseEvent evt)
				{
					currentPeg.x = evt.getX()-45;
					currentPeg.y = evt.getY()-45;
				}
			});
			
			addMouseListener(new java.awt.event.MouseAdapter()
		     {
				public void mousePressed(java.awt.event.MouseEvent evt)
				{
					int x = evt.getX();
					int y = evt.getY();
					
					for(int i = 0; i < spaces.length; i ++)
					{
						for(int j = 0; j < spaces[i].length; j++)
						{
							if(x > spaces[i][j].x && x < spaces[i][j].x+spaces[i][j].width && y > spaces[i][j].y && y < spaces[i][j].y+spaces[i][j].height)
							{
								if(p.getPegs()[i][j])
								{
									startPeg[0] = i;
									startPeg[1] = j;
									validStart = true;
									movingPeg = true;
								}
							}
						}
					}
					
					currentPeg.x = evt.getX()-45;
					currentPeg.y = evt.getY()-45;
					
				}
				
				public void mouseReleased(java.awt.event.MouseEvent evt)
				{
					int x = evt.getX();
					int y = evt.getY();
					
					for(int i = 0; i < spaces.length; i++)
					{
						for(int j = 0; j < spaces[i].length; j++)
						{
							if(x > spaces[i][j].x && x < spaces[i][j].x+spaces[i][j].width && y > spaces[i][j].y && y < spaces[i][j].y+spaces[i][j].height)
							{
								endPeg[0] = i;
								endPeg[1] = j;
								validEnd = true;
							}
						}
					}
					
					if(validStart && validEnd)
					{
						p.move(startPeg, endPeg);
					}
					
					movingPeg = false;
					validStart = false;
					validEnd = false;
					startPeg[0] = -1;
				}
			});
			
		}
		
		
		public void paintComponent(Graphics gr)
		{
			super.paintComponent(gr);
			Graphics2D gr2 = (Graphics2D)gr;
			
			for(int i = 0; i < spaces.length; i++)
			{
				for(int j = 0; j < spaces[i].length; j++)
				{
					gr2.setColor(Color.BLUE);
					gr2.fill(spaces[i][j]);
					gr2.setColor(Color.BLACK);
					gr2.draw(spaces[i][j]);
				}
			}
			
			boolean[][]pegs = p.getPegs();
			
			gr2.setColor(Color.GREEN);
			for(int i = 0; i < pegs.length; i++)
			{
				for(int j = 0; j < pegs[i].length; j++)
				{
					if(pegs[i][j] && !(startPeg[0] == i && startPeg[1] == j))
						gr2.fill(myPegs[i][j]);
				}
			}
			
			if(movingPeg)
				gr2.fill(currentPeg);
		}
	}
	
	/** Creates a new instance of PegGameGUI */
	public PegGameGUI()
	{
		super("Peg Game");
		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
		
		add(new DisplayPanel());
		setLocation(100, 100);
		setSize(600, 600);
		setVisible(true);
		
		
		addKeyListener(new java.awt.event.KeyAdapter()
		{
		     public void keyTyped(java.awt.event.KeyEvent evt)
		     {
		          System.out.println("KEY TYPED");
				if(evt.getKeyChar() == 'h')
				{
					int [][] myMove = ai.getNextMove(p.getPegs());
					p.move(myMove[0], myMove[1]);		
				}
			}
		});
		
		displayer = new Thread(this, "displayer");
		displayer.start();
	}
	
	private void myKeyTyped(java.awt.event.KeyEvent evt)
	{
		
	}
	

	/**
	 * Runs the game
	 */
	public void run()
	{
		p = new PegGameBoard();
		ai = new PegGameAI();

		JOptionPane.showMessageDialog(null, "INSTRUCTIONS:\n1. Click on any peg to begin the game\n2. Click and drag a piece to make a move\n3. If you get stuck,\n  press 'h' for some help (AI)");
	
		while(!gameOver)
		{
			
			
			try
			{
				displayer.sleep(30);
			}
			catch(InterruptedException ie)
			{}
			
			if(p.numMovesAvailable() == 0 && ai.numPegsLeft(p.getPegs()) < 15)
			{
				gameOver = true;
			}
			
			repaint();
		}
		
		int score = ai.numPegsLeft(p.getPegs());
		
		if(score == 1)
		{
			JOptionPane.showMessageDialog(null, "YOU WIN!!!!\nYour score: " + score);
		}
		else if(score == 2)
		{
			JOptionPane.showMessageDialog(null, "ALMOST THERE!\nYour score: " + score);
		}
		else if(score == 3)
		{
			JOptionPane.showMessageDialog(null, "YOU ARE PRETTY GOOD!\nYour score: " + score);
		}
		else
		{
			JOptionPane.showMessageDialog(null, "YOU NEED SOME PRACTICE!\nYour score: " + score);
		}
		
		this.setVisible(false);
		dispose();
	}
	
	
		
	public static void main(String[] args) throws IOException
	{
		PegGameGUI pg = new PegGameGUI();
	}
}
